package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    //添加购物车
    @PostMapping("/cart/add")
    public ResultInfo save(@RequestBody Cart cart){
        Cart cartFromDB = cartService.save(cart);

        //之所以要返回这个cart对象,是因为前端需要从这个对象中获取number来显示
        return ResultInfo.success(cartFromDB);
    }

    //查看购物车
    @GetMapping("/cart/list")
    public ResultInfo findList(){
        List<Cart> cartList = cartService.findList();
        return ResultInfo.success(cartList);
    }

    //清空购物车
    @DeleteMapping("/cart/clean")
    public ResultInfo clean(){
        cartService.clean();
        return ResultInfo.success(null);
    }

    //修改购物车,减数量
    @PostMapping("/cart/sub")
    public ResultInfo sub(@RequestBody Map<String,Long> map){
        Long dishId = map.get("dishId");
        Long setmealId = map.get("setmealId");
        Cart cart = cartService.sub(dishId, setmealId);
        return ResultInfo.success(cart);
    }
}
